package com.fyb.springcloud.controller;

import com.fyb.springcloud.service.CacheService;
import com.fyb.springcloud.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @author fyb
 * @version 1.0
 * @date 2021/3/11 16:45
 */
@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    /**
     * @param key
     * @param value
     * @param expireTime
     * @return boolean
     * @Author fyb
     * @Description //TODO 保存数据到 redis
     * @Date 14:22 2021/3/11
     **/
    @PostMapping("/save2Redis/{key}/{value}/{expireTime}")
    //@HystrixCommand(fallbackMethod = "")
    public boolean save2Redis(@PathVariable("key") String key, @PathVariable("value") String value, @PathVariable("expireTime") long expireTime) {
        RedisUtils.checkNull(key);
        RedisUtils.checkNull(value);
        return cacheService.save2Redis(key, value, expireTime);
    }

    /**
     * @param key
     * @return java.lang.String
     * @Author fyb
     * @Description //TODO 从reids中获取数据
     * @Date 15:58 2021/3/11
     **/
    @PostMapping("/getFromRedis/{key}")
    public String getFromRedis(@PathVariable("key") String key) {
        return cacheService.getFromRedis(key);
    }
    /**
     * @Author fyb
     * @Description //TODO  删除key 中的数据
     * @Date 12:03 2021/3/13
     * @param key
     * @return boolean
     **/
    @PostMapping("/cache/delete/{key}")
    public boolean deleteKey(@PathVariable("key") String key) throws Exception{
        return cacheService.deleteKey(key);
    }
    /**
     * @param key
     * @param expireTime
     * @return boolean
     * @Author fyb
     * @Description //TODO 给指定的key 设置过期时间
     * @Date 15:59 2021/3/11
     **/
    @PostMapping("/expire/{key}/{expireTime}")
    public boolean expire(@PathVariable("key") String key, @PathVariable("expireTime") long expireTime) {
        return cacheService.expire(key, expireTime);
    }

    /**
     * @param key
     * @return java.lang.Long
     * @Author fyb
     * @Description //TODO 获取自增后的数据
     * @Date 16:00 2021/3/11
     **/
    @PostMapping("/save2Redis/{key}")
    public Long getAutoIncrementId(@PathVariable("key") String key) {
        return cacheService.getAutoIncrementId(key);
    }

    /**
     * @param key
     * @return java.util.Set<java.lang.Object>
     * @Author fyb
     * @Description //TODO 获取指定key中的set的集合
     * @Date 16:01 2021/3/11
     **/
    @GetMapping("/sMembers/{key}")
    public Set<Object> sMembers(@PathVariable("key") String key) {
        return cacheService.sMembers(key);
    }

    /**
     * @param key
     * @param value
     * @return java.lang.Long
     * @Author fyb
     * @Description //TODO 向set中添加数据
     * @Date 16:02 2021/3/11
     **/
    @PostMapping("/sAdd/{key}/{value}")
    public Long sAdd(@PathVariable("key") String key, @PathVariable("value") String value) {
        return cacheService.sAdd(key, value);
    }

    /**
     * @param key
     * @param values
     * @return java.lang.Long
     * @Author fyb
     * @Description //TODO 批量向set中添加数据
     * @Date 16:03 2021/3/11
     **/
    @PostMapping("/sAdds/{key}/{values}")
    public Long sAdds(@PathVariable("key") String key, @PathVariable("values") String[] values) {
        return cacheService.sAdd(key,values);
    }

    /**
     * @param key
     * @param value
     * @return java.lang.Long
     * @Author fyb
     * @Description //TODO 删除指定的数据
     * @Date 16:04 2021/3/11
     **/
    @PostMapping("/sRemove/{key}/{value}")
    public Long sRemove(@PathVariable("key") String key, @PathVariable("value") String value) {
        return cacheService.sRemove(key, value);
    }

    /**
     * @param key
     * @param filed
     * @param value
     * @return void
     * @Author fyb
     * @Description //TODO 向hash中存放某个数据
     * @Date 16:05 2021/3/11
     **/
    @PostMapping("/hSet/{key}/{field}/{value}")
    public void hSet(@PathVariable("key") String key, @PathVariable("field") String filed, @PathVariable("value") String value) {
        cacheService.hSet(key, filed, value);
    }

    /**
     * @param key
     * @param filed
     * @return java.lang.String
     * @Author fyb
     * @Description //TODO 从hash中获取指定field的数据
     * @Date 16:06 2021/3/11
     **/
    @GetMapping("/hGet/{key}/{field}")
    public String hGet(@PathVariable("key") String key, @PathVariable("field") String filed) {
        return cacheService.hGet(key, filed);
    }

    /**
     * @param key
     * @return java.util.Map<java.lang.Object, java.lang.Object>
     * @Author fyb
     * @Description //TODO 获取 hash 中所有的数据
     * @Date 16:07 2021/3/11
     **/
    @GetMapping("/hGetAll/{key}")
    public Map<Object, Object> hGetAll(@PathVariable("key") String key) {
        return cacheService.hGetAll(key);
    }

    /**
     * @param key
     * @param value
     * @param expireSecond
     * @return boolean
     * @Author fyb
     * @Description //TODO setnx操作
     * @Date 16:09 2021/3/11
     **/
    @PostMapping("/setNX/{key}/{value}/{expireSecond}")
    public boolean setNX(@PathVariable("key") String key, @PathVariable("value") String value, @PathVariable("expireSecond") long expireSecond) {
        return cacheService.setNX(key, value, expireSecond);
    }

    /**
     * @param key
     * @param map
     * @return boolean
     * @Author fyb
     * @Description //TODO 批量添加
     * @Date 16:11 2021/3/11
     **/
    @PostMapping("/hMset/{key}")
    public boolean hMset(@PathVariable("key") String key, @RequestBody Map<String, Object> map) {
        return cacheService.hMset(key, map);
    }

    /**
     * @param pattern
     * @return java.util.Set<java.lang.String>
     * @Author fyb
     * @Description //TODO 查找符合表达式 key key *
     * @Date 16:12 2021/3/11
     **/
    @GetMapping("/selectByPattern/{pattern}")
    public Set<String> selectByPattern(@PathVariable("pattern") String pattern) {
        return cacheService.selectByPattern(pattern);
    }

    /**
     * @param key
     * @param field
     * @param delta
     * @return java.lang.Long
     * @Author fyb
     * @Description //TODO redis中hash 数据的自增操作
     * @Date 16:13 2021/3/11
     **/
    @PostMapping("/hIncrement/{key}/{field}/{delta}")
    public Long hIncrement(@PathVariable("key") String key, @PathVariable("field") String field, @PathVariable("delta") long delta) {
        return cacheService.hIncrement(key, field, delta);
    }


}
